package com.appkubes.fintech.restful.application.rent.controller;

import com.appkubes.fintech.common.exption.BusinessException;
import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.core.po.FintechRentOrder;
import com.appkubes.fintech.restful.application.credit.service.CreditService;
import com.appkubes.fintech.restful.application.pay.common.contract.response.RentOrderResponse;
import com.appkubes.fintech.restful.application.rent.reponse.RentOrderDTO;
import com.appkubes.fintech.restful.application.rent.request.order.AddRentOrderVO;
import com.appkubes.fintech.restful.application.rent.request.OrderKeyVO;
import com.appkubes.fintech.restful.application.rent.service.IRePayService;
import com.appkubes.fintech.restful.application.rent.service.IRentOrderService;
import com.appkubes.fintech.restful.application.rent.service.impl.RePayServiceImple;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Author: Rongjie
 * @Date: 2020/1/5 13:58
 * @Description:
 */

@Slf4j
@Api(tags = "租赁订单信息")
@RestController
@RequestMapping("/rent/order/")
public class RentOrderController {
    @Resource
    IRentOrderService rentOrderService;
    @Resource
    CreditService creditService;
    @Resource
    RePayServiceImple rePayService;

    /**
     * 从电商商城接收订单，扣信用额度, 完成租赁的逻辑
     *
     * @param addRentOrderVO
     * @param bindingResult
     * @return
     */
    @ApiOperation("添加订单")
    @PostMapping("/add")
    public Response<RentOrderDTO> addOrder(@RequestBody @Valid AddRentOrderVO addRentOrderVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }
        if(!creditService.isRefuseExpire(addRentOrderVO.getUserKey())){
            return Response.fail("您的信用不足");
        }
        RentOrderDTO orderAddDTO = rentOrderService.addRentOrder(addRentOrderVO);
        return Response.success(orderAddDTO);
    }

    @ApiOperation("查询租聘单状态接口")
    @GetMapping(value = "queryRentOrder")
    public Response<RentOrderResponse> queryRentOrder(String orderKey){
       return Response.success(rentOrderService.queryOrderKey(orderKey)) ;
    }

    /**
     * 订单发货，会生成租赁单的账单
     *
     * @return
     */

    @ApiOperation("支付状态更新")
    @PostMapping("/paySuc")
    public Response paySuc(String orderKey) {
        return  rentOrderService.paySuc(orderKey);
    }


    /**
     * 订单发货，会生成租赁单的账单
     *
     * @param orderKeyVO
     * @param bindingResult
     * @return
     */

    @ApiOperation("发货")
    @PostMapping("/ship")
    public Response<RentOrderDTO> ship(@Valid OrderKeyVO orderKeyVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }
        RentOrderDTO orderAddDTO = rentOrderService.ship(orderKeyVO);

        return Response.success(orderAddDTO);
    }
    /**
     * 取消订单，需要检查是否满足取消条件
     *
     * @param orderKeyVO
     * @param bindingResult
     * @return
     */
    @ApiOperation("取消订单")
    @PostMapping("/cancel")
    public Response<RentOrderDTO> cancel(@Valid OrderKeyVO orderKeyVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }
        RentOrderDTO orderAddDTO = rentOrderService.cancel(orderKeyVO);
        return Response.success(orderAddDTO);
    }

//    /**
//     * 取消订单，需要检查是否满足取消条件
//     *
//     */
//    @ApiOperation("取消订单")
//    @GetMapping("/test")
//    public Response test() {
//
//         FintechRentOrder ore=   rentOrderService.queryByUserKey("5ee7338c9d06462d9c26e83b73c029e9");
//        return Response.success();
//    }

    /**
     * 上付租金默认付第一期
     * @return
     */
    @ApiOperation("付第一期")
    @GetMapping("/payFirst")
    public Response payFirst(String billKey) {
        System.out.println("billKey>>>>:" + billKey);
        Response res = rePayService.repayFirst(billKey);
        if (res.getCode() != 200) {
            throw new BusinessException(res.getMessage());
        }
        return res;
    }
}
